package cz.proks.easyhelpdesk.gwt.restapi;

import java.util.Collection;

import org.jsonmaker.gwt.client.Jsonizer;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONValue;

import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.model.User;
import cz.proks.easyhelpdesk.api.model.UserJsonizer;
import cz.proks.easyhelpdesk.gwt.utils.JsonizerUtils;
import cz.proks.easyhelpdesk.gwt.utils.RestUtils;

public class UserRestApi extends BaseRestApi<User> {
    private final static Jsonizer JSONIZER = GWT.create(UserJsonizer.class);

    @Override
    protected String getBaseRestDomainUrlPart() {
        return Configuration.WS.USER_BASE_URL;
    }

    @Override
    protected Jsonizer getDomainJsonizer() {
        return JSONIZER;
    }    
    
    public void getForProvider(String providerId, final DomainCallback<User> callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/getForProvider/" + providerId, new RestUtils.Callback(callback) {
            
            @Override
            public void onSuccess(JSONValue val) {
                User user = JsonizerUtils.getEntity(JSONIZER, val);
                callback.onDomainSuccess(user);
            }
        });
    }
    
    public void listForHelpdesk(Long helpdeskId, final DomainListCallback<User> callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/listForHelpdesk/" + helpdeskId, new RestUtils.Callback(callback) {
            
            @Override
            public void onSuccess(JSONValue val) {
                Collection<User> users = JsonizerUtils.getListFromJsonArray(val, JSONIZER);
                callback.onDomainListSuccess(users);
            }
        });
    }
}
